package com.zhangmeng.online.exam.ui.components;

import com.alibaba.fastjson2.JSONObject;
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhangmeng.online.exam.ui.service.ExamService;
import javafx.stage.Stage;
import javafx.stage.Window;

/**
 * 考试按钮组件
 *
 * @author zm
 * @date 2025/3/21 13:59
 * @version: 1.0
 */
public class ExamButtonComponent extends FlowPane {

    private ExamComponent examComponent;

    private SimpleIntegerProperty countButton = new SimpleIntegerProperty(0);

    private final Map<Button, Integer> buttonMap = new HashMap<>();


    public ExamButtonComponent(int count, ExamComponent examComponent) {

        this.getStylesheets().add(getClass().getResource("/css/button.css").toExternalForm());
        this.examComponent = examComponent;
        this.countButton.set(count);
        this.setHgap(10);
        this.setVgap(10);
        this.setPadding(new Insets(10));
        for (int i = 1; i <= count; i++) {
            Button button = new Button(String.valueOf(i));
            button.setPrefWidth(45);
            button.setOnAction(event -> {
                //滚动到指定题目
                int currentIndex = buttonMap.get(button);
                examComponent.scrollToIndex(currentIndex);
            });
            buttonMap.put(button, i);
            Node node = examComponent.getNode(i);

            if (node instanceof SingleChoiceComponent singleChoiceComponent) {
                singleChoiceComponent.setIndexButton(button);
            }
            if (node instanceof MultiChoiceComponent multiChoiceComponent) {
                multiChoiceComponent.setIndexButton(button);
            }
            if (node instanceof TrueFalseComponent judgementComponent) {
                judgementComponent.setIndexButton(button);
            }
            if (node instanceof FillBlankComponent fillBlankComponent) {
                fillBlankComponent.setIndexButton(button);
            }
            if (node instanceof ShortAnswerComponent shortAnswerComponent) {
                shortAnswerComponent.setIndexButton(button);
            }

            this.getChildren().add(button);
        }

        HBox hBox = new HBox();
        hBox.prefWidthProperty().bind(this.widthProperty().subtract(40));
        Button commit = new Button("提交");

        commit.setStyle("-fx-background-color: #008CBA;");
        commit.setTextFill(Paint.valueOf("#FFFFFF"));
        commit.setPrefWidth(100);
        commit.setOnAction(event -> {
            VBox vBox = this.examComponent.getvBox();
            ObservableList<Node> children = vBox.getChildren();
            List<Map<String, Object>> params = new ArrayList<>();
            for (Node child : children) {
                if (child instanceof SingleChoiceComponent) {
                    SingleChoiceComponent question = (SingleChoiceComponent) child;
                    if (question.getNumber_of_responses() == 0) {
                        AlertUtils.alert_warning("您尚未作答该题目！题目：" + question.getQuestionLabel().getText() + "，请先作答！");
                        break;
                    }
                    params.add(question.getContext());
                }
                if (child instanceof MultiChoiceComponent) {
                    MultiChoiceComponent question = (MultiChoiceComponent) child;
                    if (question.getNumber_of_responses() == 0) {
                        AlertUtils.alert_warning("您尚未作答该题目！题目：" + question.getQuestionLabel().getText() + "，请先作答！");
                        break;
                    }
                    params.add(question.getContext());
                }
                if (child instanceof TrueFalseComponent) {
                    TrueFalseComponent question = (TrueFalseComponent) child;
                    if (question.getNumber_of_responses() == 0) {
                        AlertUtils.alert_warning("您尚未作答该题目！题目：" + question.getQuestionLabel().getText() + "，请先作答！");
                        break;
                    }
                    params.add(question.getContext());
                }
                if (child instanceof FillBlankComponent) {
                    FillBlankComponent question = (FillBlankComponent) child;
                }
                if (child instanceof ShortAnswerComponent) {
                    ShortAnswerComponent question = (ShortAnswerComponent) child;
                    if (question.getAnswerArea().getText().isEmpty()) {
                        AlertUtils.alert_warning("您尚未作答该题目！题目：" + question.getQuestionLabel().getText() + "，请先作答！");
                        break;
                    }
                    Map<String, Object> context = question.getContext();
                    context.put("answer_content", question.getAnswerArea().getText());
                    params.add(context);
                }
            }

            String jsonString = JSONObject.toJSONString(params);
            boolean flag = ExamService.submitExam(jsonString);
            if (flag) {
                AlertUtils.alert_msg("提交成功！");
                //closeDialog();
            } else {
                AlertUtils.alert_warning("提交失败！");
            }

        });
        hBox.getChildren().add(commit);
        hBox.setAlignment(Pos.CENTER);
        this.getChildren().add(hBox);
    }

    public void closeDialog() {
        Scene scene = examComponent.getScene();
        Stage window = (Stage) scene.getWindow();
        window.close();
    }
}
